package com.cskaoyan.market.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.service.MarketCouponService;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.util.CouponCode;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;

/**
 * @author tangle
 * @since 2023/03/21 14:53
 */
public class MarketCouponServiceImpl implements MarketCouponService {

    @Override
    public List<MarketCoupon> list(int page, int limit, String name, int goodsId, String sort, String order) {
        //获取sqlSession
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample couponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = couponExample.createCriteria();
        //构建查询条件
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike(name);
        }
        if(goodsId != 0){
            criteria.andIdEqualTo(goodsId);
        }
        //设置排序规则
        couponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        //select * from tb where xxx order by add_time desc
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(couponExample);
        session.commit();
        session.close();
        return marketCoupons;
    }

    @Override
   public void insertCoupon(String code, String days, String desc, String discount, String limit, String name, LocalDateTime endtime, LocalDateTime starttime, String min, Short goodsType, Short timeType, Short total, Short type, String tag){
//获取sqlSession
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setDays ( Short.valueOf ( days ) );
        marketCoupon.setCode ( code );
        marketCoupon.setDesc ( desc );
        BigDecimal bdiscount=new BigDecimal(discount);
//设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
        bdiscount=bdiscount.setScale(2, BigDecimal.ROUND_HALF_UP);
        marketCoupon.setDiscount ( bdiscount );
        marketCoupon.setEndTime ( endtime );
        marketCoupon.setStartTime ( starttime  );
        // marketCoupon.setAddTime ( LocalDateTime.now ()  );
        // marketCoupon.setUpdateTime ( LocalDateTime.now () );
        marketCoupon.setTimeType ( timeType );
        marketCoupon.setLimit ( Short.valueOf ( limit ) );
        marketCoupon.setName ( name );
        BigDecimal bmin=new BigDecimal(min);
        marketCoupon.setMin ( bmin );
        marketCoupon.setGoodsType ( goodsType );
        marketCoupon.setStatus ( CouponCode.STATUS );
        marketCoupon.setTag ( tag );
        marketCoupon.setTotal ( Integer.valueOf ( total ) );
        marketCoupon.setType ( type );
        couponMapper.insertSelective ( marketCoupon );
        session.commit();
        session.close();
    }
}
